package com.prp.gwt.client.user.article;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.Window;
import com.prp.gwt.client.ClientConstants;
import com.prp.gwt.client.Host;
import com.prp.gwt.client.widgets.UserBarPanel;
import com.prp.gwt.client.widgets.user.article.ArticleEditPanel;
import com.prp.gwt.client.widgets.user.article.ArticleView;
import com.prp.gwt.client.widgets.user.article.EntityRecommendPanel;
import com.prp.gwt.client.widgets.user.article.RecommendPanel;

public class ArticleHistoryHandler implements ValueChangeHandler<String> {

	public static final String ARTICLE = "article";
	public static final String PREFIX_ADD_RECOMMEND = "addrecommend=";
	public static final String PREFIX_CREATE_ENTITY = "addentity=";
	public static final String PREFIX_ENTITY = "entity=";
	public static final String PREFIX_RECOMMEND = "recommend=";
	public static final String INIT_VALUE = ARTICLE;

	@Override
	public void onValueChange(ValueChangeEvent<String> event) {
		String token = event.getValue();
		if (token.equals(ARTICLE)) {
			UserBarPanel.getInstance().selectTab(ClientConstants.INDEX_ARTICLE);
			ArticleView.getInstance().showWidget(ArticleView.ARTICLE);
		} else if (token.startsWith(PREFIX_ADD_RECOMMEND)) {
			try {
				UserBarPanel.getInstance().selectTab(
						ClientConstants.INDEX_ARTICLE);
				int entityId = Integer.parseInt(token
						.substring(PREFIX_ADD_RECOMMEND.length()));
				ArticleListener.getInstance().getEntity(entityId);
				ArticleEditPanel.getInstance().init();
				ArticleView.getInstance().showWidget(ArticleView.ADD_RECOMMEND);
			} catch (NumberFormatException e) {
				Window.Location.replace(Host.getPAGE_NOT_FOUND());
			}
		} else if (token.startsWith(PREFIX_CREATE_ENTITY)) {
			try {
				int categoryId = Integer.parseInt(token
						.substring(PREFIX_CREATE_ENTITY.length()));
				UserBarPanel.getInstance().selectTab(
						ClientConstants.INDEX_ARTICLE);
				ArticleListener.getInstance().getSubjectChildCategoryList(
						categoryId, null);
				ArticleView.getInstance().showWidget(ArticleView.CREATE_ENTITY);
			} catch (NumberFormatException e) {
				Window.Location.replace(Host.getPAGE_NOT_FOUND());
			}
		} else if (token.startsWith(PREFIX_ENTITY)) {
			try {
				int entityId = Integer.parseInt(token.substring(PREFIX_ENTITY
						.length()));
				UserBarPanel.getInstance().selectTab(
						ClientConstants.INDEX_ARTICLE);
				EntityRecommendPanel.getInstance().init();
				ArticleListener.getInstance().getEntity(entityId);
				ArticleListener.getInstance().getSubjectRecommendList(entityId);
				ArticleView.getInstance().showWidget(ArticleView.ENTITY);
			} catch (NumberFormatException e) {
				Window.Location.replace(Host.getPAGE_NOT_FOUND());
			}
		} else if (token.startsWith(PREFIX_RECOMMEND)) {
			try {
				int subjectId = Integer.parseInt(token
						.substring(PREFIX_RECOMMEND.length()));
				UserBarPanel.getInstance().selectTab(
						ClientConstants.INDEX_ARTICLE);
				RecommendPanel.getInstance().init();
				ArticleListener.getInstance().getSubjectRecommend(subjectId);
				ArticleListener.getInstance().getCommentList(subjectId);
				ArticleListener.getInstance().addOneClick(subjectId);
				ArticleView.getInstance().showWidget(ArticleView.RECOMMEND);
			} catch (NumberFormatException e) {
				Window.Location.replace(Host.getPAGE_NOT_FOUND());
			}
		}
	}
}
